<template>
  <div class="app-container">
    <basic-table
      :table-title="tableTitle"
      :data-source="dataSource"
      :search-form="searchForm"
      :button-group="buttonGroup"
      :multiple-table="false"
      :reload.sync="reload"
      @closeReload="closeReload"
    />
    <el-dialog
      v-if="dialogFormVisible"
      :title="getTitle"
      :visible.sync="dialogFormVisible"
      top="3%"
      @close="cancel"
    >
      <div class="el-dialog-div">
        <data-form
          v-if="dialogFormVisible"
          :loading="loading"
          :edit-data="editData"
          :operation-type="operationType"
          @createDataEmit="createDataEmit"
          @dialogFormVisibleEmit="dialogFormVisibleEmit"
        />
      </div>
    </el-dialog>
  </div>
</template>
<script>
import BasicTable from '@/components/BasicTable/index.vue'
import DataForm from '@/views/organization/company/components/dataForm.vue'

import { GetCompanyPage, AddCompany, UpdateCompany } from '@/api/Company'
import { jsonToFormData } from '@/utils'
import { TipsBox } from '@/utils/feedback'

export default {
  name: 'Organization',
  components: { BasicTable, DataForm },
  data() {
    return {
      loading: false,
      reload: false,
      dialogFormVisible: false,
      dataSource: () => GetCompanyPage,
      tableTitle: [
        {
          label: '公司名称',
          value: 'CompanyName',
          show: true
        },
        {
          label: '邮箱',
          value: 'Email',
          show: true
        },
        {
          label: '电话',
          value: 'Telephone',
          show: true
        },
        {
          label: '传真',
          value: 'Fax',
          show: true
        },
        {
          label: '备注',
          value: 'Remark',
          show: true
        },
        {
          label: '操作',
          show: true,
          type: 'options',
          width: '120',
          options: [
            {
              text: '编辑',
              icon: 'el-icon-edit',
              type: 'success',
              callback: row => {
                this.dialogFormVisible = true
                this.operationType = 'edit'
                this.editData = row
              }
            }
          ]
        }
      ],
      searchForm: {
        expend: true,
        title: '表格筛选',
        size: 'small',
        fields: [
          {
            show: true,
            type: 'input',
            label: '公司名称',
            labelShow: true,
            name: 'companyName'
          }
        ]
      },
      buttonGroup: {
        expend: true,
        show: true,
        title: '表格筛选',
        size: 'small',
        fields: [
          {
            showButtonGroup: true,
            text: '添加',
            icon: 'el-icon-check',
            callback: type => {
              this.dialogFormVisible = true
              this.operationType = 'add'
            }
          }
        ]
      },
      editData: {},
      operationType: 'add'
    }
  },
  computed: {
    getTitle() {
      return this.operationType === 'add' ? '添加' : '编辑'
    }
  },
  methods: {
    closeReload(val) {
      this.reload = val
    },
    cancel() {
      this.dialogFormVisible = false
    },
    dialogFormVisibleEmit(v) {
      this.dialogFormVisible = v
    },
    // add
    createDataEmit(v) {
      const formData = jsonToFormData(v)
      let operate = null

      if (this.operationType === 'add') {
        // 注册
        operate = AddCompany
      } else if (this.operationType === 'edit') {
        // 编辑
        operate = UpdateCompany
      }

      if (!operate) {
        return
      }

      this.loading = true
      operate(formData).then((res) => {
        this.loading = false
        let type = 'error'
        if (res.statusCode === 200) {
          type = 'success'
          this.dialogFormVisible = false
          this.reload = true
        }
        TipsBox(type, res.message)
      }).catch(err => {
        console.log('error', err)
        this.loading = false
      })
    }
  }
}
</script>
